package org.docmanagement.actions;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.docmanagement.commons.struts.CommonAction;
import org.docmanagement.data.Element;
import org.docmanagement.forms.SearchForm;

/**
 * Created on 10 nov. 2004
 * @author eveno
 */
public class SearchAction extends CommonAction
{
    /**
     * @see org.docmanagement.commons.struts.BaseDispatchAction#createMethodMap()
     */
    protected void createMethodMap()
    {
        methodMap = new HashMap<String, String>();
        methodMap.put("Prepare", "list");
        methodMap.put("Rechercher", "search");
    }
    /**
     * @see org.docmanagement.commons.struts.BaseDispatchAction#list(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception
    {
        addKeywordList(request);
        return mapping.findForward(SEARCH);
    }
    /**
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception
    {
    	// TODO : Ajouter les filtres par type de document
        SearchForm searchForm = (SearchForm) form;
        List<? extends Element> elements = null;
        if (StringUtils.isNotEmpty(searchForm.getTitlePattern()))
        {
            String pattern = convertPattern(searchForm.getTitlePattern());
            log.info("Searching by title, pattern: "+searchForm.getTitlePattern());
            elements = getElementService().findByTitle(pattern);
        }
        else if (StringUtils.isNotEmpty(searchForm.getContentPattern()))
        {
            String pattern = convertPattern(searchForm.getContentPattern());
            log.info("Searching by content, pattern: "+searchForm.getContentPattern());
            elements = getElementService().findByContent(pattern);
        }
        else if (StringUtils.isNotEmpty(searchForm.getDescriptionPattern()))
        {
            String pattern = convertPattern(searchForm.getDescriptionPattern());
            log.info("Searching by content, pattern: "+searchForm.getDescriptionPattern());
            elements = getElementService().findByDescription(pattern);
        }
        else if (searchForm.getKeywordIds().length != 0)
        {
            log.info("Searching by keywords, keywordIds: "+searchForm.getKeywordIds());
            elements = getElementService().findByKeywordIds(searchForm.getKeywordIds());
        }
        addElements(request, elements);
        return mapping.findForward(SUCCESS);
    }
    /**
     * @param contentPattern
     * @return
     */
    private String convertPattern(String pPattern)
    {
    	if (pPattern.indexOf("*") != -1)
    		return pPattern.replace('*', '%');
    	return '%'+pPattern+'%';
    }
}
